function setHotWords(hotwordsStr){
    var api = "/backstage/system/hotwords";
    var data = {
        "words": hotwordsStr,
    };
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: "POST",
        data: data,
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status == 200) {
                console.log("请求成功");
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    })
}

function getHotWords(){
    var api = "/backstage/system/hotwords";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200) {
                var hotwordStr = data.data.join(" | ");
                if(hotwordStr.trim().length > 0){
                    $("#hotwordInput").val(hotwordStr);
                }
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    })
}

$(function(){
    getHotWords();
    $("#saveHotwordBtn").on("click", function(){
        var hotwordsStr = $("#hotwordInput").val();
        setHotWords(hotwordsStr);
    });
})